package com.yanghx.order.web;

import com.yanghx.order.dao.OrderRepository;
import com.yanghx.order.domain.Order;
import com.yanghx.service.IOrderService;
import com.yanghx.service.dto.OrderDto;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yangHX
 * createTime  2019/8/10 11:29
 */
@RestController
@RequestMapping(value = "/api/order")
public class OrderResource implements IOrderService {
    @Resource
    private OrderRepository orderRepository;

    public void init() {
        Order order = new Order();
        order.setAmount(100);
        order.setTitle("MyOrder");
        order.setDetail("Bought a imooc course");
        orderRepository.save(order);
    }


    @Override
    public OrderDto create(@RequestBody OrderDto dto) {
        Order order = new Order();
        order.setAmount(dto.getAmount());
        order.setTitle(dto.getTitle());
        order.setDetail(dto.getDetail());
        order = orderRepository.save(order);
        dto.setId(order.getId());
        return dto;
    }

    @GetMapping("/{id}")
    @Override
    public OrderDto getMyOrder(@PathVariable Long id) {
        Order order = orderRepository.findOne(id);
        OrderDto dto = new OrderDto();
        dto.setId(order.getId());
        dto.setAmount(order.getAmount());
        dto.setTitle(order.getTitle());
        dto.setDetail(order.getDetail());
        return dto;
    }

    @GetMapping("")
    public List<Order> getAll() {
        return orderRepository.findAll();
    }
}
